'use strict';

const Controller = require('egg').Controller;

const jwt = require('jsonwebtoken');

class UserController extends Controller {
  async login() {
    let {ctx,service} = this;
    let {username,password} = ctx.request.body;
    if(username && password){
        //存在
        try{
            let data = await service.user.login(username,password);
            console.log("data",data)
            if(data.length){
                //查到了
                let token = jwt.sign({username,password,roleid:data[0].roleid},'1705d',{expiresIn:60*60})  //默认是秒
                ctx.body = {
                    code:1,
                    token,
                    rolename:data[0].rolename
                }
            }else{
                ctx.body = {
                    code:0,
                    msg:'用户名或密码有误'
                }
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
        
    }else{
        ctx.body = {
            code:2,
            msg:'缺失参数'
        }
    }
  }
}

module.exports = UserController;
